﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class OrderDAO
    {
        static DataClasses1DataContext Data = new DataClasses1DataContext();

        public static bool insertOrder(ORDER oOrder)
        {
            try
            {
                Data.ORDERs.InsertOnSubmit(oOrder);
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static List<ORDER> GetOrderByMember(int IDMember)
        {
            try
            {
                var query = from order in Data.ORDERs where ((order.IDMEMBER == IDMember)&& (order.VISIBLE==true))select order;
                List<ORDER> ListOrder = new List<ORDER>();
                foreach (ORDER order in query)
                {
                    ListOrder.Add(order);
                }
                return ListOrder;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public static List<ORDER> GetOrderByProduct(int IDProduct)
        {
            try
            {
                var query = from order in Data.ORDERs where ((order.IDPRODUCT == IDProduct)&&(order.VISIBLE==true)) select order;
                List<ORDER> ListOrder = new List<ORDER>();
                foreach (ORDER order in query)
                {
                    ListOrder.Add(order);
                }
                return ListOrder;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool IsExisting(int IDOrder)
        {
            try
            {
                return Data.ORDERs.Any(Order => ((Order.IDORDER == IDOrder) && (Order.VISIBLE == true)));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Delete(int IDOrder)
        {
            try
            {
                ORDER Order = Data.ORDERs.Single(or => or.IDORDER == IDOrder);
                Order.VISIBLE = false;
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static ORDER GetOrder(int idOrder)
        {
            try
            {
                return Data.ORDERs.Single(or => or.IDORDER == idOrder);
            }
            catch
            {
                return null;
            }
        }

        public static bool Update(ORDER Order)
        {
            try
            {
                ORDER EditOrder = Data.ORDERs.Single(or => or.IDORDER == Order.IDORDER);
                EditOrder.STATUS_ORDER = Order.STATUS_ORDER;
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
